package com.qixin.modules.rzInvoice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qixin.modules.ea.entity.EaBookDetail;
import com.qixin.modules.ea.mapper.EaBookDetailMapper;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.entity.EaBillItem;
import com.qixin.modules.rzInvoice.mapper.EaBillItemMapper;
import com.qixin.modules.rzInvoice.mapper.EaBillMapper;
import com.qixin.modules.rzInvoice.service.IEaBillService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.nc.mapper.NcBdTaxcodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: ea_bill
 * @Author: qixin-boot
 * @Date:   2021-09-10
 * @Version: V1.0
 */
@Service
public class EaBillServiceImpl extends ServiceImpl<EaBillMapper, EaBill> implements IEaBillService {
    
    @Autowired
    private EaBookDetailMapper eaBookDetailMapper;
    @Autowired
    private EaBillItemMapper eaBillItemMapper;
    @Autowired
    private NcBdTaxcodeMapper ncBdTaxcodeMapper;
    
    @Override
    public EaBookDetail queryEaBookDetail(String id) {
        List<EaBookDetail> eaBookDetails = eaBookDetailMapper.selectByMainId(id);
        if(eaBookDetails.size()>0){
            EaBookDetail eaBookDetail = eaBookDetails.get(0);
            return eaBookDetail;
        }else{
            return null;
        }
    }

    @Override
    public void updateEabill(EaBookDetail eaBookDetail) {
        eaBookDetailMapper.updateById(eaBookDetail);
    }

    @Override
    public List<Map<String,Object>> getBillDetailList(String fpid) {
            // 获取发票信息
            EaBill eaBill = this.baseMapper.selectById(fpid);
            // 获取发票类型中文名称
            String typeName = this.baseMapper.selectTypeName(eaBill.getType());
            // 通过发票id 查询对应的发票明细行
            List<EaBillItem> items = eaBillItemMapper.selectByPid(fpid);
            List<BigDecimal> taxRates = new ArrayList<>();

            if(items != null && items.size() > 0 && (!"10506".equals(eaBill.getType()))){
                List<Map<String,Object>> mapList = new ArrayList<>();
                for (int i = 0;i < items.size();i++){
                    EaBillItem eaBillItem = items.get(i);
                    BigDecimal taxRate = eaBillItem.getTaxRate() == null ? new BigDecimal("0") : eaBillItem.getTaxRate();
                    if(i == 0){ // 第一行明细
                        taxRates.add(taxRate);
                        // 获取税码主键
                        String taxCode = ncBdTaxcodeMapper.getTaxId(taxRate);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("fpTypeName",typeName); // 发票类型
                        map.put("taxCode",taxCode); // 税码主键
                        map.put("taxRate",taxRate); // 税率
                        map.put("tax",eaBillItem.getTax()); // 税额
                        map.put("pretaxAmount",eaBillItem.getTotal()); // 税前金额
                        map.put("totalAmount",eaBillItem.getTotalAmount()); // 税价金额
                        mapList.add(map);
                    }else{
                        for (int n = 0;n < taxRates.size();n++){
                            int res = taxRate.compareTo(taxRates.get(n));
                            if(res == 0){ // 税率一致
                                List<Map<String, Object>> list = mapList.stream().filter(map -> taxRate.compareTo((BigDecimal) map.get("taxRate")) == 0).collect(Collectors.toList());
                                Map<String, Object> map1 = list.get(0);
                                map1.put("tax",eaBillItem.getTax().add(new BigDecimal(map1.get("tax").toString()))); // 税额
                                map1.put("pretaxAmount",eaBillItem.getTotal().add(new BigDecimal(map1.get("pretaxAmount").toString()))); // 税前金额
                                map1.put("totalAmount",eaBillItem.getTotalAmount().add(new BigDecimal(map1.get("totalAmount").toString()))); // 税价金额
                                break;
                            }
                            if(n == taxRates.size()-1){ // 遍历了一边税率数组，没有发现相同的税率
                                taxRates.add(taxRate);
                                // 获取税码主键
                                String taxCode = ncBdTaxcodeMapper.getTaxId(taxRate);
                                HashMap<String, Object> map = new HashMap<>();
                                map.put("fpTypeName",typeName); // 发票类型
                                map.put("taxCode",taxCode); // 税码主键
                                map.put("taxRate",taxRate); // 税率
                                map.put("tax",eaBillItem.getTax()); // 税额
                                map.put("pretaxAmount",eaBillItem.getTotal()); // 税前金额
                                map.put("totalAmount",eaBillItem.getTotalAmount()); // 税价金额
                                mapList.add(map);
                                break;
                            }
                        }
                    }
                }
                return mapList;
            }else{
                List<Map<String,Object>> mapList = new ArrayList<>();
                HashMap<String, Object> map = new HashMap<>();
                // 飞机票
                if("10506".equals(eaBill.getType())){
                    map.put("fare",eaBill.getFare()); // 票价
                    map.put("fuelSurcharge",eaBill.getFuelSurcharge()); // 燃油费
                    String taxCode = ncBdTaxcodeMapper.getTaxId(eaBill.getTaxRate());
                    map.put("fpTypeName",typeName); // 发票类型
                    map.put("taxCode",taxCode); // 税码主键
                    map.put("taxRate",eaBill.getTaxRate()); // 税率
                }else{
                    // 获取税码主键
                    String taxCode = ncBdTaxcodeMapper.getTaxId(eaBill.getTaxRate());
                    map.put("fpTypeName",typeName); // 发票类型
                    map.put("taxCode",taxCode); // 税码主键
                    map.put("tax",eaBill.getTax()); // 税额
                    map.put("taxRate",eaBill.getTaxRate()); // 税率
                    map.put("pretaxAmount",eaBill.getPretaxAmount());// 税前金额
                    if (eaBill.getIsNotTax()){ // 如果是不计税，那直接取发票金额作为 税前金额
                        map.put("pretaxAmount",eaBill.getTotal());//
                    }
                    map.put("totalAmount",eaBill.getTotal()); // 价税合计
                }
                mapList.add(map);
                return mapList;
            }
    }
}
